package com.mlethe.library.imagepreview;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;
import android.widget.VideoView;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.core.graphics.Insets;
import androidx.core.view.OnApplyWindowInsetsListener;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.fragment.app.FragmentActivity;

public class GPVideoPlayerActivity extends FragmentActivity {

    VideoView videoView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent = getIntent();
        String url = intent.getStringExtra("url");
        boolean isFullscreen = intent.getBooleanExtra("isFullscreen", false);
        if (isFullscreen) {
            Window window = getWindow();
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.VANILLA_ICE_CREAM) {
                EdgeToEdge.enable(this);
                window.setNavigationBarContrastEnforced(false);
            } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                // 状态栏透明
                window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
                int flag = View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    flag = flag | View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
                }
                window.getDecorView().setSystemUiVisibility(flag);
                window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                window.setStatusBarColor(Color.TRANSPARENT);
                window.setNavigationBarColor(Color.BLACK);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
                    // 异形屏延伸显示区域
                    WindowManager.LayoutParams params = window.getAttributes();
                    params.layoutInDisplayCutoutMode =
                            WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
                    window.setAttributes(params);
                }
            }
        }
        View view = getLayoutInflater().inflate(R.layout.activity_gpvideoplayer, null);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.VANILLA_ICE_CREAM) {
            View statusBarView = view.findViewById(R.id.statusBarView);
            View navigationBarView = view.findViewById(R.id.navigationBarView);
            ViewCompat.setOnApplyWindowInsetsListener(view, new OnApplyWindowInsetsListener() {

                boolean isUpdate = false;

                @NonNull
                @Override
                public WindowInsetsCompat onApplyWindowInsets(@NonNull View v, @NonNull WindowInsetsCompat insets) {
                    Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars() | WindowInsetsCompat.Type.displayCutout());
                    if (isUpdate) return insets;
                    isUpdate = true;
                    navigationBarView.setPadding(0, 0, 0, systemBars.bottom);
                    if (!isFullscreen) {
                        statusBarView.setPadding(0, systemBars.top, 0, 0);
                    }
                    return insets;
                }
            });
        }
        setContentView(view);
        videoView = findViewById(R.id.gpVideo);
        videoView.setVideoPath(url);
        videoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {
            @Override
            public boolean onError(MediaPlayer mp, int what, int extra) {
                Toast.makeText(GPVideoPlayerActivity.this, R.string.playback_failed, Toast.LENGTH_SHORT).show();
                return false;
            }
        });
        videoView.start();

    }

    @Override
    protected void onResume() {
        super.onResume();
        if (!videoView.isPlaying()) {
            videoView.start();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        videoView.pause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        videoView.stopPlayback();
    }

    /***
     * 启动播放视频
     * @param   context context
     * @param  url url
     * @param  isFull isFull
     * ***/
    public static void startActivity(Context context, String url, boolean isFull) {
        Intent intent = new Intent(context, GPVideoPlayerActivity.class);
        intent.putExtra("url", url);
        intent.putExtra("isFullscreen", isFull);
        context.startActivity(intent);
    }
}
